//
// Created by goldfish on 24/3/25.
//
#include <iostream>
#include <string>

#include "databaseManager.h"
#include "sqlite3.h"

#include "filesystem"
namespace fs=std::filesystem;
DatabaseManager::DatabaseManager(const string& db_path,const string& db_name)
:db_path(db_path)
{
    fs::create_directories(fs::path(this->db_path).parent_path());

    if(sqlite3_open((db_path+db_name).c_str(), &db) != SQLITE_OK) {
        throw runtime_error("Can't open database: " + string(sqlite3_errmsg(db)));
    }
    create_table();
    if(sqlite3_open((db_path+db_name).c_str(), &db) != SQLITE_OK) {
        throw runtime_error("Can't open database: " + string(sqlite3_errmsg(db)));
    }
    create_table();
}

void DatabaseManager:: create_table() {
    const char* sql =
            "CREATE TABLE IF NOT EXISTS tty_login ("
            "tty_name TEXT PRIMARY KEY,"
            "tty_login_times INTEGER DEFAULT 0);";

    execute_sql(sql);
}
